package components.propertyCtrls
{
	import UIFree.controls.uif_label;
	import UIFree.controls.uif_list;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class c_listCtrl extends c_propertyBase {
		public static const WEIGHT: Number = 127;
		private static const ITEMHEIGHT: int = 30;
		private var _list: uif_list;
		public function c_listCtrl(parent:uif_component, aData:Object, y:Number, a_height:Number) {
			super(parent, aData, y, a_height);
		}
		
		override protected function createComponents(): void {
			super.createComponents();
			var inner: Rectangle = componentRect;
			_list = new uif_list(this, inner.x, inner.y, inner.width, inner.height, 'list');
			addEvent(Event.CHANGE, doChange, _list);
		}
		
		public function get list(): uif_list {
			return _list;
		}
		
		override protected function getValue():Object {
			return _list.selectIndex;
		}
		
		override protected function setData(value:Object):void {
			super.setData(value);
			if (_data.values) {
				_list.clearList();
				_list.attachList(_data.values, createItem);
				_list.selectIndex = _data.value;
			}
		}
		
		protected function createItem(area: uif_ecomponent, a_item: Object, i: int): uif_ecomponent {
			var rect: Rectangle = area.getInner();
			var item: uif_label = new uif_label(area, rect.x, rect.y + i * ITEMHEIGHT, rect.width, ITEMHEIGHT, _list.style.itemClass);
			item.text = getCaption(a_item);
			return item
		}
		
		protected function getCaption(a_item: Object): String {
			return (a_item is String)?(a_item as String):((a_item.hasOwnProperty('caption')?a_item.caption:''));
		}
	}
}